package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.enums.BizCodeEnum;
import com.exception.BizException;
import com.mapper.UserMapper;
import com.model.LoginUser;
import com.model.UserDO;
import com.request.UserLoginRequest;
import com.request.UserRegisterRequest;
import com.service.UserService;
import com.utils.CommonUtil;
import com.utils.JsonData;
import com.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author rkc
 * @date 2022/8/24 10:16
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 注册
     * @param userRegisterRequest
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public JsonData register(UserRegisterRequest userRegisterRequest) {
        UserDO userDO = new UserDO();
        BeanUtils.copyProperties(userRegisterRequest, userDO);
        userDO.setRegisterTime(new Date());
        if (StringUtils.isBlank(userDO.getHeadImg())) {
            userDO.setHeadImg("https://static01.teamcode.com/upload/3c6f438c0b76/a6cded4f-f282-4f80-befe-1ec5e41a8b11.jpg?x-oss-process=image/resize,m_fill,w_600");
        }
        //密码加密
        //生成秘钥，盐
        userDO.setSecret("$1$" + CommonUtil.getStringNumRandom(8));
        //密码+盐处理
        String cryptPwd = Md5Crypt.md5Crypt(userRegisterRequest.getPassword().getBytes(), userDO.getSecret());
        userDO.setPassword(cryptPwd);
        try {
            userMapper.insert(userDO);
            log.info("注册成功：{}", userDO);
            return JsonData.buildSuccess();
        } catch (DuplicateKeyException e) {
            throw new BizException(BizCodeEnum.ACCOUNT_REPEAT);
        }
    }

    /**
     * 登录
     * @param userLoginRequest
     * @return
     */
    @Override
    public JsonData login(UserLoginRequest userLoginRequest) {
        UserDO userDO = userMapper.selectOne(new QueryWrapper<UserDO>().eq("username", userLoginRequest.getUsername()));
        if (userDO == null) {
            return JsonData.buildResult(BizCodeEnum.ACCOUNT_UNREGISTER);
        }
        String cryptPwd = Md5Crypt.md5Crypt(userLoginRequest.getPassword().getBytes(), userDO.getSecret());
        if (cryptPwd.equals(userDO.getPassword())) {
            LoginUser loginUser = new LoginUser();
            BeanUtils.copyProperties(userDO, loginUser);
            //生成token
            String token = JwtUtil.generateJsonWebToken(loginUser);
            return JsonData.buildSuccess(token);
        }
        return JsonData.buildResult(BizCodeEnum.ACCOUNT_PWD_ERROR);
    }
}
